package com.qiye.livevideo.controller;

import com.qiye.livevideo.common.CodeMsg;
import com.qiye.livevideo.common.ResultInfo;
import com.qiye.livevideo.common.RetConstant;
import com.qiye.livevideo.utils.AliyunOSSUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created on 2019/5/1.
 *
 * @author meishijun
 */
@Api(description = "文件管理")
@RestController
public class FileController {
    private static final Logger log = LoggerFactory.getLogger(FileController.class);
    /**
     * 文件上传
     * @param file
     */
    @ApiOperation(value = "文件上传")
    @RequestMapping(value = "/fileUpload",method = RequestMethod.POST)
    public ResultInfo uploadFile(MultipartFile file, @ApiParam(name = "accessKeyId", value = "AccessKeyId是类似身份的标识") @RequestParam(required = false) String accessKeyId) {
        try {
            List<String> fileList = new ArrayList<String>();
            //获取上传文件原名
            String filename = file.getOriginalFilename();
            if (!"".equals(filename.trim())) {
                //指定路径，解决linux没有写入权限
                String fileUrlstr = RetConstant.FILE_PATH + System.currentTimeMillis() + filename;
                File newFile = new File(fileUrlstr);
                if (!newFile.getParentFile().exists()) {
                    newFile.getParentFile().mkdirs();
                }
                file.transferTo(newFile);
                //上传到OSS阿里云
                String objectName = AliyunOSSUtil.generalUploadObject(newFile,accessKeyId);
                fileList.add(AliyunOSSUtil.getUrl(objectName,accessKeyId));
                newFile.delete();
            }
            return ResultInfo.success(fileList);
        } catch (Exception e) {
            log.error("文件上传失败：" + e.getMessage());
            return ResultInfo.error(CodeMsg.SERVER_EXCEPTION, e.getMessage());
        }
    }

    /**
     * 分片上传测试
     * @param file
     * @return
     */
    @ApiOperation(value = "大文件上传")
    @RequestMapping(value = "/fragmentationUpload", method = RequestMethod.POST)
    public ResultInfo uploadFileFen(MultipartFile file,@ApiParam(name = "accessKeyId", value = "AccessKeyId是类似身份的标识") @RequestParam(required = false) String accessKeyId) {
        try {
            List<String> fileList = new ArrayList<String>();
            //获取上传文件原名
            String filename = file.getOriginalFilename();
            if (!"".equals(filename.trim())) {
                //指定路径，解决linux没有写入权限
                String fileUrlstr = RetConstant.FILE_PATH + System.currentTimeMillis() + filename;
                File newFile = new File(fileUrlstr);
                if (!newFile.getParentFile().exists()) {
                    newFile.getParentFile().mkdirs();
                }
                // 转存文件
                file.transferTo(newFile);
                String objectName = AliyunOSSUtil.multipartUpload(newFile,accessKeyId);
                fileList.add(AliyunOSSUtil.getUrl(objectName,accessKeyId));
                newFile.delete();
            }
            return ResultInfo.success(fileList);
        } catch (Exception e) {
            log.error("大文件分片上传失败：" + e.getMessage());
            return ResultInfo.error(CodeMsg.SERVER_EXCEPTION, e.getMessage());
        }
    }


}
